package com.kly.cashmall.module.order.presenter;

import com.google.gson.JsonArray;
import com.kly.cashmall.bean.BaseEntity;
import com.kly.cashmall.bean.ExtensionDetailBean;
import com.kly.cashmall.bean.LoanDetailBean;
import com.kly.cashmall.bean.RazorpayInfoBean;
import com.kly.cashmall.framework.mvp.BaseViewPresenter;
import com.kly.cashmall.services.Network;
import com.kly.cashmall.services.Params;
import com.kly.cashmall.utils.function.ToastUtil;
import com.kly.cashmall.widget.Loading.LoadingDialog;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * @author
 */
public class LoanDetailsPresenter extends BaseViewPresenter<LoanDetailsViewer> {

    public LoanDetailsPresenter(LoanDetailsViewer viewer) {
        super(viewer);
    }

    @Override
    public void willDestroy() {

    }

    public void getOrderDetail(String tradeNo){
        LoadingDialog.showLoading(getActivity());
        Params params = new Params();
        params.put("tradeNo", tradeNo);
        rxManager.add(Network.getApi().getOrderDetail(params.buildPostBody()).subscribeOn(Schedulers.io()).observeOn
                (AndroidSchedulers.mainThread()).subscribe(new Consumer<LoanDetailBean>() {
            @Override
            public void accept(LoanDetailBean bean) throws Exception {
                LoadingDialog.dismissLoading();
                if (getViewer() != null) {
                    getViewer().getOrderDetailSuccess(bean);
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                LoadingDialog.dismissLoading();
                ToastUtil.showAppToast(throwable.getMessage());
            }
        }));
    }

    public void repayRezorpayPost(String tradeNo) {
        LoadingDialog.showLoading(getActivity());
        Params params = new Params();
        params.put("tradeNo", tradeNo);
        rxManager.add(Network.getApi().getOrderRepay(params.buildPostBody()).subscribeOn(Schedulers.io()).observeOn
                (AndroidSchedulers.mainThread()).subscribe(new Consumer<RazorpayInfoBean>() {
            @Override
            public void accept(RazorpayInfoBean bean) throws Exception {
                LoadingDialog.dismissLoading();
                if (getViewer() != null) {
                    getViewer().repayPostSuccess(bean);
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                LoadingDialog.dismissLoading();
                ToastUtil.showAppToast(throwable.getMessage());

            }
        }));
    }




    public void getExtensionDetail(JsonArray bill) {
        Params params = new Params();
        params.put("pendingBillNos", bill);
        rxManager.add(Network.getApi().getExtensionDetail(params.buildPostBody()).subscribeOn(Schedulers.io()).observeOn
                (AndroidSchedulers.mainThread()).subscribe(new Consumer<ExtensionDetailBean>() {
            @Override
            public void accept(ExtensionDetailBean bean) throws Exception {
                if (getViewer() != null) {
                    getViewer().getExtensionDetailSuccess(bean);
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                ToastUtil.showAppToast(throwable.getMessage());

            }
        }));
    }

    public void getOrderWithdraw(String tradeNo){
        Params params = new Params();
        params.put("tradeNo", tradeNo);
        rxManager.add(Network.getApi().getOrderWithdraw(params.buildPostBody()).subscribeOn(Schedulers.io()).observeOn
                (AndroidSchedulers.mainThread()).subscribe(new Consumer<BaseEntity>() {
            @Override
            public void accept(BaseEntity bean) throws Exception {
                if (getViewer() != null) {
                    getViewer().getOrderWithdrawSuccess(bean.getMsg());
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                ToastUtil.showAppToast(throwable.getMessage());

            }
        }));
    }
}